#include "StdAfx.h"
#include ".\stationinfo.h"

StationInfo::StationInfo(const StationInfo& rhs)
:StationPath(rhs.StationPath)
,StationName ( rhs.StationName)
,SerialNumber( rhs.SerialNumber)
,UniformSerialNumber ( rhs.UniformSerialNumber)
,LastRecordTime ( rhs.LastRecordTime)
,value ( rhs.value)
{
}

StationInfo::StationInfo( const CString& _StationPath,const CString& _StationName, const CString& _SerialNumber, const CString& _UniformSerialNumber, const COleDateTime& _LastRecordTime, float _value )
:StationPath(_StationPath)
,StationName ( _StationName)
,SerialNumber( _SerialNumber)
,UniformSerialNumber ( _UniformSerialNumber)
,LastRecordTime ( _LastRecordTime)
,value ( _value)
{
}

StationInfo::~StationInfo(void)
{
}


StationInfo& StationInfo::operator=( const StationInfo& rhs )
{
	StationPath      = rhs.StationPath;
	StationName = rhs.StationName;
	SerialNumber= rhs.SerialNumber;
	UniformSerialNumber = rhs.UniformSerialNumber;
	LastRecordTime = rhs.LastRecordTime;
	value = rhs.value;
	return *this;
}

void StationInfo::SaveStationName()
{
	CString oldPath = StationPath;

	char drive[256];
	char dir[256];
	char fname[256];
	char ext[256];

	_splitpath(StationPath.GetBuffer(),drive,dir,fname,ext);
	
	//Change the StationPath
	StationPath.Format("%s%s%s",drive,dir,StationName);
	
	//Change the Dir name
	CFile::Rename(oldPath,StationPath);
}

void StationInfo::SaveSerialNumber()
{
	CString strPath_Setting;
	if( HasBackslash(StationPath))
		strPath_Setting.Format("%sstation.cfg",StationPath);
	else
		strPath_Setting.Format("%s\\station.cfg",StationPath);

	WritePrivateProfileString("SETTING","SN" , SerialNumber.GetBuffer(), strPath_Setting.GetBuffer());
}

void StationInfo::SaveUniformNumber()
{
	CString strPath_Setting;
	if( HasBackslash(StationPath))
		strPath_Setting.Format("%sstation.cfg",StationPath);
	else
		strPath_Setting.Format("%s\\station.cfg",StationPath);

	WritePrivateProfileString("SETTING","USN", UniformSerialNumber.GetBuffer(), strPath_Setting.GetBuffer());
}

void StationInfo::SaveLastRecordTime()
{
	CString strPath_Logging;
	if( HasBackslash(strPath_Logging))
		strPath_Logging.Format("%sRecord.log",StationPath);
	else
		strPath_Logging.Format("%s\\Record.log",StationPath);
	
	//2008.11.12 19:00:00
	CString strTime = LastRecordTime.Format("%Y.%m.%d %H:%M:%S");
	WritePrivateProfileString("RECORD","LASTTIME", strTime.GetBuffer(), strPath_Logging.GetBuffer()); 
}

void StationInfo::SaveValue()
{
	CString strPath_Logging;
	if( HasBackslash(strPath_Logging))
		strPath_Logging.Format("%sRecord.log",StationPath);
	else
		strPath_Logging.Format("%s\\Record.log",StationPath);

	CString strValue ;
	strValue.Format("%.2f",value);
	WritePrivateProfileString("RECORD","VALUE", strValue.GetBuffer(), strPath_Logging.GetBuffer()); 
}

void StationInfo::SetStationName( const CString& name )
{
	StationName = name;
	SaveStationName();
}

void StationInfo::SetSerialNumber( const CString& sn )
{
	SerialNumber = sn;
	SaveSerialNumber();
}

void StationInfo::SetUniformNumber( const CString& USN )
{
	UniformSerialNumber = USN;
	SaveUniformNumber();
}

void StationInfo::SetLastRecordTime( const COleDateTime& time )
{
	LastRecordTime = time;
	SaveLastRecordTime();
}

void StationInfo::SetValue( float rhs )
{
	value = rhs;
	SaveValue();
}